package com.example.ccbb;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ccbb.entity.InfoData;
import com.example.ccbb.okhttp.GetData;
import com.example.ccbb.okhttp.GetImg;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button btnLeft;
    private Button btnRight;
    private Button btnSave;
    private TextView tvTitle;
    private TextView tvAuthor;
    private TextView tvContent;
    private ImageView imageView;
    private String URL_PATH = "http://www.imooc.com/api/teacher?type=3&cid=";
    private String IMG_PATH = "http://www.imooc.com/api/teacher?type=1";
    private int count = 1;
    private List<InfoData> infoList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        getImg();

        String url = URL_PATH + count;
        loadData(url);

        listener();

    }

    private void getImg() {
        loadImg(IMG_PATH);
    }

    private void listener() {
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (count <= 30) {
                    count++;
                    String url = URL_PATH + count;
                    loadData(url);
                } else {
                    Toast.makeText(getApplicationContext(), "已经到最后一篇啦", Toast.LENGTH_LONG).show();
                }
            }
        });

        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count--;
                if (count >= 1) {
                    String url = URL_PATH + count;
                    loadData(url);
                } else {
                    Toast.makeText(getApplicationContext(), "已经到第一篇了", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void loadData(String url) {
        GetData key = new GetData(url);
        String result = key.getData();
        if (result == null) {
            Log.e("zfm", "网络出问题了");
        } else {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String str = jsonObject.optString("data");
                JSONObject resultTwo = new JSONObject(str);
                String title = resultTwo.getString("title");
                String author = resultTwo.getString("author");
                String content = resultTwo.getString("content");
                tvTitle.setText(title);
                tvContent.setText(content);
                tvAuthor.setText(author);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private void loadImg(String url) {
        GetData key = new GetData(url);
        String result = key.getData();
        if (result == null) {

        } else {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String str = jsonObject.optString("data");
                JSONObject jsonObjectTwo = new JSONObject(str);
                String pic = jsonObjectTwo.getString("pic1");
                GetImg img = new GetImg(pic);
                byte[] bytes;
                bytes = img.getImg();
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                imageView.setImageBitmap(bitmap);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private void initView() {
        btnLeft = findViewById(R.id.btn_left);
        btnRight = findViewById(R.id.btn_right);
        btnSave = findViewById(R.id.btn_save);
        tvTitle = findViewById(R.id.tv_title);
        tvAuthor = findViewById(R.id.tv_author);
        tvContent = findViewById(R.id.tv_content);
        imageView = findViewById(R.id.img);
    }
}
